package com.gulimall.product.web;

import com.gulimall.product.entity.CategoryEntity;
import com.gulimall.product.service.CategoryService;
import com.gulimall.product.vo.CatalogV2;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author kkk
 * @date 2022/7/21
 */
@Controller
public class IndexController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping({"/", "/index.html"})
    public String indexPage(Model model) {
        List<CategoryEntity> categoryEntities = categoryService.queryCategoryParentLevel();

        model.addAttribute("categorys", categoryEntities);
        return "index";
    }

    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String, List<CatalogV2>> getCatelogList() {
        return categoryService.getCatelogV2List();
    }

    @GetMapping("/hello")
    @ResponseBody
    public String hello() {
        RLock lock = redissonClient.getLock("my-lock");
        //锁会自动续期，运行期间到时间了，会自动执行定时任务 向redis续期时间
        //lock.lock();
        //传递了时间，默认超时就是设置的时间，到期后redis会自动删除，不会续期
        lock.lock(10, TimeUnit.SECONDS);
        //最佳实战    lock.lock(10, TimeUnit.SECONDS);  省掉了整个续期操作，手动解锁
        try {
            System.out.println("当前线程获得锁" + lock.getName());
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
            System.out.println("线程释放锁" + lock.getHoldCount());
        }
        return "hello";
    }

    @GetMapping("/read")
    @ResponseBody
    public String readValue() {
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw-lock");
        RLock rLock = readWriteLock.readLock();
        String UUID = "";
        try {
            rLock.lock();
            System.out.println("读锁加锁成功" + Thread.currentThread().getId());
            Thread.sleep(10000);
            UUID = redisTemplate.opsForValue().get("writeValue");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            rLock.unlock();
            System.out.println("读锁释放 " + Thread.currentThread().getId());
        }
        return UUID;
    }

    @GetMapping("/write")
    @ResponseBody
    public String writeValue() {
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw-lock");
        RLock rLock = readWriteLock.writeLock();
        String UUID = "";
        try {
            rLock.lock();
            UUID = java.util.UUID.randomUUID().toString();
            System.out.println("写锁加锁成功 " + Thread.currentThread().getId());
            Thread.sleep(10000);
            redisTemplate.opsForValue().set("writeValue", UUID);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            rLock.unlock();
            System.out.println("写锁释放 " + Thread.currentThread().getId());
        }
        return UUID;
    }

    @GetMapping("/lockDoor")
    @ResponseBody
    public String lockDoor() throws InterruptedException {
        RCountDownLatch downLatch = redissonClient.getCountDownLatch("door");
        downLatch.trySetCount(5);
        downLatch.await();

        return "关门";
    }

    @GetMapping("/countDown")
    @ResponseBody
    public String countDown() {
        RCountDownLatch downLatch = redissonClient.getCountDownLatch("door");
        downLatch.countDown();

        return "123 " + downLatch.getCount();
    }

    @GetMapping("/park")
    @ResponseBody
    public String park() throws InterruptedException {
        RSemaphore park = redissonClient.getSemaphore("park");
        park.acquire();
        return "OK";
    }

    @GetMapping("/go")
    @ResponseBody
    public String goPark() {
        RSemaphore park = redissonClient.getSemaphore("park");
        park.release();
        return "OK";
    }

    @GetMapping("/setPark")
    @ResponseBody
    public void setPark() {
        RSemaphore park = redissonClient.getSemaphore("park");
        park.trySetPermits(3);
    }
}
